package com.uzai.console.service.pyq.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.pyq.pyqcommenttemplate.PyqCommentTemplateDto;
import com.uzai.console.dto.pyq.pyqcommenttemplate.PyqCommentTemplateIdDto;
import com.uzai.console.dto.pyq.pyqcommenttemplate.PyqCommentTemplateQuery;
import com.uzai.console.entity.PyqCommentTemplate;
import com.uzai.console.mapper.AlarmContacterMapper;
import com.uzai.console.mapper.PyqCommentTemplateMapper;
import com.uzai.console.service.pyq.PyqCommentTemplateService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.pyq.pyqcommenttemplate.PyqCommentTemplateVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 朋友圈评论模板
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PyqCommentTemplateServiceImpl implements PyqCommentTemplateService {

    Logger logger = LoggerFactory.getLogger(PyqCommentTemplateServiceImpl.class);

    @Autowired
    private PyqCommentTemplateMapper pyqCommentTemplateMapper;

    /**
     * 查询朋友圈评论模板列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqCommentTemplateQuery
     * @return null
     */
    public Page<PyqCommentTemplateVo> findPyqCommentTemplateList(PyqCommentTemplateQuery pyqCommentTemplateQuery){
        //新建返回到页面的LIST对象
        List<PyqCommentTemplateVo> pyqCommentTemplatePageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PyqCommentTemplate> page = new Page<>();
        pyqCommentTemplateQuery.convert(page);
        ArrayList<PyqCommentTemplate> pyqCommentTemplateList =  pyqCommentTemplateMapper.findByList(page, pyqCommentTemplateQuery);
        if(pyqCommentTemplateList != null && pyqCommentTemplateList.size() > 0){
            for (PyqCommentTemplate pyqCommentTemplate : pyqCommentTemplateList){
                PyqCommentTemplateVo pyqCommentTemplatePageVo = new PyqCommentTemplateVo();
                BeanUtils.copyProperties(pyqCommentTemplate, pyqCommentTemplatePageVo);
                pyqCommentTemplatePageVoList.add(pyqCommentTemplatePageVo);
            }
        }

        //返回到页面分页对象
        Page<PyqCommentTemplateVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(pyqCommentTemplatePageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询朋友圈评论模板详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqCommentTemplateIdDto
     * @return null
     */
    public PyqCommentTemplateVo findPyqCommentTemplate(PyqCommentTemplateIdDto pyqCommentTemplateIdDto){
        if(pyqCommentTemplateIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        PyqCommentTemplate pyqCommentTemplate = pyqCommentTemplateMapper.selectById(pyqCommentTemplateIdDto.getId(), pyqCommentTemplateIdDto.getMerId());

        if(pyqCommentTemplate == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该朋友圈评论模板不存在");
        }

        PyqCommentTemplateVo pyqCommentTemplateVo = new PyqCommentTemplateVo();
        BeanUtils.copyProperties(pyqCommentTemplate, pyqCommentTemplateVo);

        return pyqCommentTemplateVo;
    }

    /**
     * 添加朋友圈评论模板
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqCommentTemplateDto
     * @return null
     */
    public void addPyqCommentTemplate(PyqCommentTemplateDto pyqCommentTemplateDto){

        if(pyqCommentTemplateDto.getPlatform() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请选择平台");
        }

        if(StringUtils.isBlank(pyqCommentTemplateDto.getName())){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入模板名称");
        }

        //验证评论内容
        String contentStr = pyqCommentTemplateDto.getContent();
        if(StringUtils.isBlank(contentStr)){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加评论内容");
        }

        //判断必须有一条不为空的评论
        List<String> contentList = JSONObject.parseArray(contentStr, String.class);
        if(contentList == null || contentList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "必须有一条评论内容");
        }

        if(contentList.size() > 5){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "评论内容最多只能有5条");
        }

        if(contentList != null && contentList.size() > 0){
            for (String content : contentList){
                if(StringUtils.isBlank(content)){
                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "评论内容不允许为空");
                }
            }
        }

        Integer now = DateUtil.getNowTime();
        PyqCommentTemplate pyqCommentTemplate = new PyqCommentTemplate();
        pyqCommentTemplate.setId(IdWorker.getId());
        pyqCommentTemplate.setMerId(pyqCommentTemplateDto.getMerId());
        pyqCommentTemplate.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
        pyqCommentTemplate.setPlatform(pyqCommentTemplateDto.getPlatform());
        pyqCommentTemplate.setName(pyqCommentTemplateDto.getName());
        pyqCommentTemplate.setContent(pyqCommentTemplateDto.getContent());
        pyqCommentTemplate.setCreateTime(now);
        pyqCommentTemplate.setUpdateTime(now);
        pyqCommentTemplateMapper.insert(pyqCommentTemplate);
    }

    /**
     * 修改朋友圈评论模板
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqCommentTemplateDto
     * @return null
     */
    public void updatePyqCommentTemplate(PyqCommentTemplateDto pyqCommentTemplateDto){
        if(pyqCommentTemplateDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(StringUtils.isBlank(pyqCommentTemplateDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入模板名称");
        }

        //验证评论内容
        String contentStr = pyqCommentTemplateDto.getContent();
        if(StringUtils.isBlank(contentStr)){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请添加评论内容");
        }

        //判断必须有一条不为空的评论
        List<String> contentList = JSONObject.parseArray(contentStr, String.class);
        if(contentList == null || contentList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "必须有一条评论内容");
        }

        if(contentList.size() > 5){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "评论内容最多只能有5条");
        }

        if(contentList != null && contentList.size() > 0){
            for (String content : contentList){
                if(StringUtils.isBlank(content)){
                    throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "评论内容不允许为空");
                }
            }
        }

        PyqCommentTemplate pyqCommentTemplate = pyqCommentTemplateMapper.selectById(pyqCommentTemplateDto.getId(), pyqCommentTemplateDto.getMerId());
        if(pyqCommentTemplate == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该评论模板不存在");
        }

        pyqCommentTemplate.setName(pyqCommentTemplateDto.getName());
        pyqCommentTemplate.setContent(pyqCommentTemplateDto.getContent());
        Integer now = DateUtil.getNowTime();
        pyqCommentTemplate.setUpdateTime(now);
        pyqCommentTemplateMapper.updateById(pyqCommentTemplate);
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param pyqCommentTemplateIdDto
     * @return null
     */
    public void deletePyqCommentTemplate(PyqCommentTemplateIdDto pyqCommentTemplateIdDto){
        if(pyqCommentTemplateIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        // TODO: 2022/3/8  查询该评论模板是否已经给朋友圈占用了

        pyqCommentTemplateMapper.deleteById(pyqCommentTemplateIdDto.getId(), pyqCommentTemplateIdDto.getMerId());
    }

    /**
     * 查询朋友圈评论模板下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<PyqCommentTemplateVo> findSelectList(PyqCommentTemplateQuery query){
        List<PyqCommentTemplateVo> pyqCommentTemplateVoList = new ArrayList<>();
        List<PyqCommentTemplate> pyqCommentTemplateList =  pyqCommentTemplateMapper.findByList(query);
        if(pyqCommentTemplateList != null && pyqCommentTemplateList.size() > 0){
            for (PyqCommentTemplate pyqCommentTemplate : pyqCommentTemplateList){
                PyqCommentTemplateVo pyqCommentTemplateVo = new PyqCommentTemplateVo();
                BeanUtils.copyProperties(pyqCommentTemplate, pyqCommentTemplateVo);
                pyqCommentTemplateVoList.add(pyqCommentTemplateVo);
            }
        }
        return pyqCommentTemplateVoList;
    }
}
